package com.sundaying.gooway.dao.impl;


import java.util.List;
import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.sundaying.gooway.dao.UrlItemDao;
import com.sundaying.gooway.entity.UrlItem;

@Component("urlItemDao")
public class UrlItemDaoImpl implements UrlItemDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public int save(UrlItem obj) {
		int result = 0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public int update(UrlItem obj) {
		int result = 0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public int delete(UrlItem obj) {
		int result = 0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public UrlItem load(long id) {
		UrlItem urlItem = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		urlItem = (UrlItem) session.load(UrlItem.class, id);
		session.getTransaction().commit();
		session.close();
		return urlItem;
	}

	@Override
	public List<UrlItem> list() {
		List<UrlItem> urlItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery("from UrlItem u");
		urlItemList = (List<UrlItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return urlItemList;
	}

	@Override
	public List<UrlItem> list(String hsql, List<Object> param) {
		List<UrlItem> urlItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hsql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}

		urlItemList = (List<UrlItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return urlItemList;
	}

	@Override
	public List<UrlItem> list(String hsql, List<Object> param, int page,
			int rows) {
		List<UrlItem> urlItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hsql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}
		// 第一条记录的序号为0
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		urlItemList = (List<UrlItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return urlItemList;
	}

	@Override
	public long count(String hql, List<Object> param) {
		long result = 0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}
		result = (Long) query.uniqueResult();
		session.getTransaction().commit();
		session.close();
		return result;
	}

	
}
